// packageI/personRed/personRedPay/personRedPay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customBalance: '', // 自定义余额字样
    info: '',
    member: '',
    popupSpecs: false,
    payPwd: '',
    isTapPay: false, // 重复支付验证
  },

  onLoad: function (options) {
    let {info, member} = options;
    info = JSON.parse(info);
    member = JSON.parse(member);
    if (!info) {
      wx.redirectTo({
        url: '/packageI/personRed/personListDetail/personListDetail',
      });
    }
    this.setData({
      info,
      member,
    });
    try {
      let yz_basic_info = wx.getStorageSync('yz_basic_info');
      this.setData({
        customBalance: yz_basic_info.balance.balance?yz_basic_info.balance.balance: "余额"
      });
    } catch (err) {
      console.log(err);
    }
  },

  onReady: function () {

  },

  onShow: function () {

  },
  payment () {
    if (this.data.isTapPay) return;
    this.setData({
      isTapPay: true
    });
    if (this.data.member.need_password === true) {
      this.setData({
        popupSpecs: true,
      });
    } else {
      this.payPost();
    }
  },

  popupSpecsClose(e){
    this.setData({
      popupSpecs:false,
      isTapPay: false
    });
  },

  onPassword(e){
    this.setData({
      payPwd:e.detail,
      isTapPay: false
    });
    if (e.detail) {
      this.getVerifyPassword(e.detail); 
    }
  },
  //验证密码
  getVerifyPassword(pwd) {
    var that = this;
    let urlStr = app.getNetAddresss("payment.password.check");
    urlStr += "&password=" + pwd;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.payPost();
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            success(restext) {
              if (restext.confirm) {
                if (res.data.code == 2001) {
                  //商城支付密码设置未开启
                } else if (res.data.code == 2002) {
                  //用户未设置支付密码
                  //去设置密码
                  wx.navigateTo({
                    url: '/packageA/member/set_balance_password/set_balance_password'
                  });
                } else if (res.data.code == 2003) {
                  //支付密码错误
                }
              } else if (restext.cancel) {
                if (res.data.code == 2001) {
                  //商城支付密码设置未开启
                } else if (res.data.code == 2002) {
                  //用户未设置支付密码
                } else if (res.data.code == 2003) {
                  //支付密码错误
                }
              }
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  payPost () {
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.redpack.send");
    let json = JSON.stringify(this.data.info);
    app._getNetWork({
      url: urlStr,
      data: {redpack_date:json},
      success: (res) => {
        this.setData({ isTapPay: false});
        if (res.data.result !== 1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
          });
          return;
        }
        wx.showToast({
          title: res.data.msg,
          mask: true,
          duration: 1000
        });
        this.timer = setTimeout(() => {
          wx.redirectTo({
            url: '/packageI/personRed/personRed/personRed',
          });
        }, 1000);
      },
      fail () {
        this.setData({ isTapPay: false});
      }
    });
  },
  onUnload () {
    clearTimeout(this.timer);
  },
});